package dao

import (
	"context"
	"core/models/db"
	"core/repo"
)

type RoleDao struct {
	repo *repo.Manager
}

func (d *RoleDao) Insert(ctx context.Context, role *db.Role) (uint, error) {
	err := d.repo.DB.Client.WithContext(ctx).Table(role.TableName()).Create(role).Error
	if err != nil {
		return 0, err
	}
	return role.ID, nil
}

func (d *RoleDao) FindRoleByRid(ctx context.Context, uid uint) *db.Role {
	role := new(db.Role)
	d.repo.DB.Client.WithContext(ctx).Table(role.TableName()).Where("uid=?", uid).First(role)
	return role
}

func (d *RoleDao) GetRole(ctx context.Context, uid uint) *db.Role {
	data := new(db.Role)
	d.repo.DB.Client.WithContext(ctx).Model(data).Where("uid=?", uid).First(data)
	return data
}
func NewRoleDao(m *repo.Manager) *RoleDao {
	return &RoleDao{
		repo: m,
	}
}
